/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.article;

import java.util.ArrayList;
import java.util.Collection;
import model.generic.TableModelWithListeners;
import util.article.SizeDetails;

/**
 *
 * @author Dirk
 */
public class SizeTableModel extends TableModelWithListeners {
    
     private final String[] columns = {"Maat"};
     private ArrayList<SizeDetails> sizes;

    public SizeTableModel() {
        sizes = new ArrayList<SizeDetails>();
    }
     
     
    @Override
    public int getRowCount() {
        return sizes.size()+1;
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(rowIndex < sizes.size())
            return sizes.get(rowIndex).getName();
        else
            return "";
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if(rowIndex < sizes.size())
            if(((String)aValue).equals(""))
                sizes.remove(rowIndex);
            else
                sizes.get(rowIndex).setName((String)aValue);
        else{
            SizeDetails size = new SizeDetails(-1, (String)aValue);
            sizes.add(size);
        }
        this.fireTableDataChanged();
    }
    
    public Collection<SizeDetails> getAllSizes(){
        return sizes;
    }
    
    public void addSizes(Collection<SizeDetails> sizeList){
        sizes.addAll(sizeList);
        this.fireTableDataChanged();
    }
    
    public void resetSizes(){
        sizes.removeAll(sizes);
        this.fireTableDataChanged();
    }

    @Override
    public void removeItem(int index) {
        sizes.remove(index);
        this.fireTableDataChanged();
    }
    
    
    
}
